﻿using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Ogdi.Config;

namespace Ogdi.DataServices
{
    public static class AppSettings
    {
        static AppSettings()
        {
            RefreshAvailableEndpoints();
        }

        internal static Dictionary<string, AvailableEndpoint> RefreshAvailableEndpoints()
        {
            var ta = CloudStorageAccount.FromConfigurationSetting("OgdiConfigConnectionString");
            var ogdiConfigContext = new OgdiConfigDataServiceContext(ta.TableEndpoint.AbsoluteUri, ta.Credentials);

            var list = ogdiConfigContext.AvailableEndpoints.ToList();

            var availableEndpoints = new Dictionary<string, AvailableEndpoint>();

            foreach (var item in list)
            {
                availableEndpoints.Add(item.alias, item);
            }

            HttpContext.Current.Cache[OgdiConfigDataServiceContext.EndpointsTableName] = availableEndpoints;

            return availableEndpoints;
        }
                
        public static string RootServiceNamespace
        {
            get
            {
                return RoleEnvironment.GetConfigurationSettingValue("RootServiceNamespace");
            }
        }

        public static string OgdiConfigTableStorageAccountName
        {
            get
            {
                return RoleEnvironment.GetConfigurationSettingValue("AccountName");
            }
        }

        public static string OgdiConfigTableStorageAccountKey
        {
            get
            {
                return RoleEnvironment.GetConfigurationSettingValue("AccountSharedKey");
            }
        }

        public static string TableStorageBaseUrl
        {
            get
            {
                return RoleEnvironment.GetConfigurationSettingValue("TableStorageBaseUrl");
            }
        }

        public static string BlobStorageBaseUrl
        {
            get
            {
                return RoleEnvironment.GetConfigurationSettingValue("BlobStorageBaseUrl");
            }
        }

        public static Dictionary<string, AvailableEndpoint> EnabledStorageAccounts
        {
            get
            {
                var enabledStorageAccounts = (Dictionary<string, AvailableEndpoint>)HttpContext.Current.Cache[OgdiConfigDataServiceContext.EndpointsTableName];
                
                // just in case the cache flushes
                if (enabledStorageAccounts == null)
                {
                    enabledStorageAccounts = RefreshAvailableEndpoints();
                }

                return enabledStorageAccounts;
            }
        }

        private const string _remainderRouteDataValue = "remainder";

        public static string RemainderRouteDataValue
        {
            get
            {
                return _remainderRouteDataValue;
            }
        }

        private const string _remainderRoutePatternSnippet = "{*" + _remainderRouteDataValue + "}";

        public static string RemainderRoutePatternSnippet
        {
            get
            {
                return _remainderRoutePatternSnippet;
            }
        }
    }
}